﻿using System; 
using System.IO; 
using System.Net;
using System.Security.Cryptography;
using System.Text;

namespace Lemonc.Tools
{
    public class HttpWebTools
    {
       
        /// <summary>
        /// 使用post方法执行 web服务
        /// </summary>
        /// <param name="url">服务地址</param>
        /// <param name="method">方法名</param>
        /// <param name="param">参数 例：flag=string&CardID=string</param>
        /// <returns></returns>
        public static string WebServicePost(string url, string method, string param)
        {
            //转换输入参数的编码类型，获取byte []数组 
            byte[] byteArray = Encoding.UTF8.GetBytes(param);
 
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url + "/" + method));
              
            webRequest.Method = "POST";
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.ContentLength = byteArray.Length;
         
            Stream newStream = webRequest.GetRequestStream(); 
            newStream.Write(byteArray, 0, byteArray.Length);
            newStream.Close();
            // 读取服务器的返回信息
            HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
            StreamReader resultStream = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            string result = resultStream.ReadToEnd();
            return result;

        }
        /// <summary>
        /// 使用post方法执行 web服务
        /// </summary>
        /// <param name="url">服务地址</param>
        /// <param name="param">参数 例：flag=string&CardID=string</param>
        /// <returns></returns>
        public static string WebServicePost(string url, string param)
        {
            //转换输入参数的编码类型，获取byte []数组 
            byte[] byteArray = Encoding.UTF8.GetBytes(param);

            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url));
           

            webRequest.Method = "POST";
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.ContentLength = byteArray.Length;

            Stream newStream = webRequest.GetRequestStream();
            newStream.Write(byteArray, 0, byteArray.Length);
            newStream.Close();
            // 读取服务器的返回信息
            HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
            StreamReader resultStream = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            string result = resultStream.ReadToEnd();
            return result;

        }
        /// <summary>
        /// 使用post方法执行 web服务
        /// </summary>
        /// <param name="url">服务地址</param>
        /// <param name="param">参数 例：flag=string&CardID=string</param>
        /// <returns></returns>
        public static string WebServicePost_1(string url, string param)
        {
            //转换输入参数的编码类型，获取byte []数组 
      

            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url+"?"+param));

            webRequest.Method = "POST";
            webRequest.ContentType = "application/x-www-form-urlencoded";

            // 读取服务器的返回信息
            HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
            StreamReader resultStream = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            string result = resultStream.ReadToEnd();
            return result;

        }
        /// <summary> 
        /// 获取时间戳 
        /// </summary> 
        /// <returns></returns> 
        public static string GetTimeStamp()
        {
            string s = "dsadasda";
            MD5 md5 = new MD5CryptoServiceProvider();
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalSeconds).ToString();
        }
       
    }
}
